package com.software.levelupserver.service.impl;

import com.software.levelupserver.entity.Task;
import com.software.levelupserver.entity.User;
import com.software.levelupserver.mappers.TaskMapper;
import com.software.levelupserver.service.TaskService;
import com.software.levelupserver.utils.DateUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {
    @Resource
    private TaskMapper taskMapper;

    /**
     * 能够获取安全认证之后的用户信息
     * @return
     */
    public User getAuthentication(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null){
            return (User) authentication.getPrincipal();
        }else {
            return null;
        }
    }

    /**
     * 新增任务（包括各种类型的任务）
     * @param task
     * @return
     */
    @Override
    public int insertTask(Task task) {

        User user = getAuthentication();

        if (user != null){
            task.setUserId(user.getUserId());
        }

        int result = 0;
        if (task.getPriority() == null){
            task.setPriority(0);
        }
        if (task.getIsRecurring() == null){
            task.setIsRecurring(0);
        }
        switch (task.getTaskType()){
            case 0: //没有设置开始时间的任务（即“待办”）
                result = taskMapper.insertTask(task);
                break;
            case 1: //通知型任务
                result = taskMapper.insertNoticeTask(task);
                break;
            case 2: //计时型任务
                if (task.getRemind2() == null){ //有一个提醒时间的计时型任务
                    result = taskMapper.insertTimingTask1(task);
                }else {
                    if (task.getRemind3() == null){ //有两个提醒时间的计时型任务
                        result = taskMapper.insertTimingTask2(task);
                    }else { //有三提醒时间的计时型任务
                        result = taskMapper.insertTimingTask3(task);
                    }
                }
                break;
            case 3: //系统任务
                result = taskMapper.insertSystemTask(task);
                break;
            default:
                break;
        }

        return result;
    }

    /**
     * 任务更新（包含各种任务）
     * @param task
     * @return
     */
    @Override
    public boolean updateTask(Task task) {

        boolean result = false;
        switch (task.getTaskType()){
            case 0: //无开始时间的任务（即待办）
                result = taskMapper.updateTask(task);
                break;
            case 1: //通知型任务
                result = taskMapper.updateNoticeTask(task);
                break;
            case 2: //计时型任务
                if (task.getRemind2() == null){ //一个提醒时间的计时型任务
                    result = taskMapper.updateTimingTask1(task);
                }else {
                    if (task.getRemind3() == null){ //两个提醒时间的计时型任务
                        result = taskMapper.updateTimingTask2(task);
                    }else { //三个提醒时间的计时型任务
                        result = taskMapper.updateTimingTask3(task);
                    }
                }
                break;
            case 3: //系统任务
                result = taskMapper.updateTimingTask2(task);
                break;
            default:
                break;
        }

        return result;
    }

    /**
     * 任务删除
     * @param task
     * @return
     */
    @Override
    public boolean deleteTask(Task task){
        return taskMapper.deleteTask(task);
    }

    /**
     * 根据userId获取所有“待办”
     * @return
     */
    @Override
    public List<Task> getAllTodoTasksByUserId() {
        User user = getAuthentication();
        List<Task> tasks;

        if (user != null)
            tasks = taskMapper.selectTodoTasks(user.getUserId());
        else
            tasks = null;

        return tasks;
    }

    /**
     * 获取指定时间的所有任务
     * @param date
     * @return
     */
    @Override
    public List<Task> getAllTasksByDate(String date) {
        User user = getAuthentication();
        List<Task> tasks;

        if (user != null)
            tasks = taskMapper.selectAllTasksByDate(date, user.getUserId());
        else
            tasks = null;

        return tasks;
    }

    /**
     * 获取指定日期和类型的任务
     * @param date
     * @param taskType
     * @return
     */
    @Override
    public List<Task> getTodayTasksByType(String date, int taskType) {
        User user = getAuthentication();
        List<Task> tasks;

        if (user != null)
            tasks = taskMapper.selectTodayTasksByType(date, user.getUserId(), taskType);
        else
            tasks = null;

        return tasks;
    }

    /**
     * 获取当天开始的所属用户的任务
     * @param date
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Task> getTodayTasks(String date, int pageNum, int pageSize) {

        //获取用户的信息
        User user = getAuthentication();

        List<Task> tasks;
        if (user != null)
            tasks = taskMapper.selectTodayTasks(date, user.getUserId(), (pageNum-1)*pageSize, pageSize);
        else
            tasks = new ArrayList<>();

        return tasks;
    }

    /**
     * 查询无开始时间的任务
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Task> getNoTimeTask(int pageNum, int pageSize) {

        User user = getAuthentication();
        List<Task> tasks;
        if (user != null){
            tasks = taskMapper.selectNoTimeTasks(user.getUserId(), (pageNum - 1) * pageSize, pageSize);
        }else {
            tasks = new ArrayList<>();
        }

        return tasks;
    }

    /**
     * 根据任务类型分页查询任务
     * @param date
     * @param taskType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Task> getTasksByTaskType(String date, int taskType, int pageNum, int pageSize) {

        User user = getAuthentication();
        List<Task> tasks;
        if (user != null){
            tasks = taskMapper.selectTasksByTaskType(date, user.getUserId(), taskType, (pageNum - 1) * pageSize, pageSize);
        }else {
            tasks = new ArrayList<>();
        }

        return tasks;
    }

    /**
     * 根据taskId查询到指定的task
     * @param taskId
     * @return
     */
    @Override
    public Task getTaskByTaskId(int taskId) {

        return taskMapper.selectTaskByTaskId(taskId);
    }

    /**
     * 通过任务状态获取任务
     * @param status
     * @return
     */
    public List<Task> getAllTasksByStatus(int status){

        User user = getAuthentication();
        List<Task> tasks;
        if (user != null){
            tasks = taskMapper.selectAllTasksByStatus(user.getUserId(), status);
        }else {
            tasks = new ArrayList<>();
        }
        return tasks;
    }

    /**
     * 通过任务优先级查询任务
     * @param priority
     * @return
     */
    @Override
    public List<Task> getTasksByPriority(String date, int priority) {
        User user = getAuthentication();
        List<Task> tasks;
        if (user != null){
            tasks = taskMapper.selectTasksByPriority(date, user.getUserId(), priority);
        }else {
            tasks = null;
        }

        return tasks;
    }


    @Override
    public List<Task> selectAllTasks() {
        return taskMapper.selectAllTasks();
    }

    /**
     * 获取本日用户添加的所有任务
     * @return
     */
    @Override
    public int countTasks() {
        int todayNum = taskMapper.countTasks(DateUtil.getNowDate("yyyy年MM月dd日"));

        return todayNum;

    }

    @Override
    public int countAllTask() {
        int allTaskNum = taskMapper.countAllTask();

        return allTaskNum;
    }

    @Override
    public List<Integer> selectCountByDate() {
        List<String> formattedThisWeekDates = DateUtil.getFormattedThisWeekDates();
        List<Integer> data = new ArrayList<>();
        for (String data2 : formattedThisWeekDates){
            data.add(taskMapper.selectCountByDate(data2));
        }
        return data;
    }

    public List<Task> getTasksByMonthAndStatus(String month){
        User user = getAuthentication();
        List<Task> tasks;
        if (user != null){
            tasks = taskMapper.selectTasksByMonthAndStatus(user.getUserId(), month);
        }else {
            tasks = null;
        }
        return tasks;
    }

}
